﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKArchMethodLibrary.Utils;
using YJKElementFinish.ColumnSurface.DataDefinition;
using TopologySearch;

namespace YJKElementFinish.ColumnSurface.Managers
{
	public class ColumnCommon
	{
		public static void GetColumns(Document doc, ref List<FamilyInstance> selColumns)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			ICollection<Element> collection = filteredElementCollector.OfCategory(BuiltInCategory.OST_Columns).OfClass(typeof(FamilyInstance)).ToElements();
			foreach (Element element in collection)
			{
				FamilyInstance item = element as FamilyInstance;
				selColumns.Add(item);
			}
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(doc, doc.ActiveView.Id);
			ICollection<Element> collection2 = filteredElementCollector2.OfCategory(BuiltInCategory.OST_StructuralColumns).OfClass(typeof(FamilyInstance)).ToElements();
			foreach (Element element2 in collection2)
			{
				FamilyInstance item2 = element2 as FamilyInstance;
				selColumns.Add(item2);
			}
		}

		public static bool GetColumnElevationInfo(FamilyInstance column, Document doc, ref double columnElevation, ref double columnHeight)
		{
			bool result;
			try
			{
				ElementId elementId = column.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_PARAM).AsElementId();
				Level level = doc.GetElement(elementId) as Level;
				double elevation = level.Elevation;
				double num = column.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM).AsDouble();
				columnElevation = elevation + num;
				ElementId elementId2 = column.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_PARAM).AsElementId();
				Level level2 = doc.GetElement(elementId2) as Level;
				double elevation2 = level2.Elevation;
				double num2 = column.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM).AsDouble();
				double num3 = elevation2 + num2;
				columnHeight = num3 - columnElevation;
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static bool GetColumnElevationInfo(FamilyInstance column, List<Level> lstLevelsContainLinkDoc, ref double columnElevation, ref double columnHeight)
		{
			bool result;
			try
			{
				ElementId elementId = column.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_PARAM).AsElementId();
				Level level = null;
				foreach (Level level2 in lstLevelsContainLinkDoc)
				{
					if (elementId.IntegerValue == level2.Id.IntegerValue)
					{
						level = level2;
						break;
					}
				}
				double elevation = level.Elevation;
				double num = column.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM).AsDouble();
				columnElevation = elevation + num;
				ElementId elementId2 = column.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_PARAM).AsElementId();
				Level level3 = null;
				foreach (Level level4 in lstLevelsContainLinkDoc)
				{
					if (elementId2.IntegerValue == level4.Id.IntegerValue)
					{
						level3 = level4;
						break;
					}
				}
				double elevation2 = level3.Elevation;
				double num2 = column.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM).AsDouble();
				double num3 = elevation2 + num2;
				columnHeight = num3 - columnElevation;
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static List<Pole2D> GetSolidProfile(Solid solid, ElementId columnId, double offset, ref List<ProfileInfo> columnProfileLoopList)
		{
			List<Pole2D> list = new List<Pole2D>();
			foreach (object obj in solid.Faces)
			{
				Face face = (Face)obj;
				if (!(face.GetType() != typeof(PlanarFace)))
				{
					PlanarFace face2 = face as PlanarFace;
					if (Geometry.IsEqual(face2.FaceNormal(), -XYZ.BasisZ))
					{
						foreach (object obj2 in face.EdgeLoops)
						{
							EdgeArray edgeArray = (EdgeArray)obj2;
							List<XYZ> list2 = new List<XYZ>();
							new List<EdgeInfo>();
							foreach (object obj3 in edgeArray)
							{
								Edge edge = (Edge)obj3;
								Curve curve = edge.AsCurve();
								Pole2D item = Pole2D.TransformWallToStructure2DPole(curve, columnId, offset, offset);
								list.Add(item);
								IList<XYZ> list3 = curve.Tessellate();
								for (int i = 0; i < list3.Count - 1; i++)
								{
									list2.Add(list3[i]);
								}
							}
							ProfileInfo item2 = new ProfileInfo(list2);
							columnProfileLoopList.Add(item2);
						}
					}
				}
			}
			return list;
		}
	}
}
